package uni.androidgame;

import java.util.ArrayList;

import uni.androidgame.gameobjects.Monster;
import uni.androidgame.gameobjects.Player;

public class GameStateUpdater
{
    private static Player _player;
    private static ArrayList<Monster> _monsters;
    private static ArrayList<Monster> _monstersToRemove = new ArrayList<Monster>();
    private static int _turn = 0;
    public static int getTurn(){return _turn;}

    public void setCharactersToUpdate(ArrayList<Monster> monsters, Player player)
    {
        _player = player;
        _monsters = monsters;
    }

    public void updateGameState()
    {
        GameAction gameAction = _player.getNextAction();
        if(gameAction != null)
        {
            executeAction(gameAction);
            for(Monster monster : _monsters)
            {
                gameAction = monster.createNextAction(_player);
                if(gameAction == null)
                {
                    _monstersToRemove.add(monster);
                    continue;
                }
                executeAction(gameAction);
            }
            _monsters.removeAll(_monstersToRemove);
            _monstersToRemove.clear();
        }
    }
    private void executeAction(GameAction action)
    {
        switch (action.getType())
        {
            case Move: executeMoveAction(action);break;
            case Attack:executeAttack(action);break;
            case Wait:break;
            case Throw:break;
            case Use:break;
        }
    }

    private void executeAttack(GameAction action)
    {
        Character defender = action.getTargetTile().getCharacter();
        Character attacker = action.getOwner();

        defender.damage(attacker.getAttackPower());
    }
    private void executeMoveAction(GameAction action)
    {
        if(action.getTargetTile().getCharacter() == null)
        {
            action.getOwner().moveTo(action.getTargetTile());
        }
    }

}
